﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace TaskDemo
{
    class Program
    {
        static ConcurrentDictionary<int, string> m_ResultMap = new ConcurrentDictionary<int, string>();

        public delegate int Math(int param1, int param2); //定义委托类型
        public static int Add(int param1, int param2)//定义同签名函数
        {
            return param1 + param2;
        }

        private static event Action action_0;

        private static event Action<int> action_1;

        private static event Action<int, double> action_2;

        private static event Action<int, double, string> action_3;

        private static void Fun_0()
        {
            Console.WriteLine("没有参数");
        }

        private static void Fun_1(int nInt)
        {
            Console.WriteLine("包含一个参数" + nInt);
        }

        private static void Fun_2(int nInt, double nDob)
        {
            Console.WriteLine($"包含2个参数:{nInt},{nDob}");
        }

        private static void Fun_3(int nInt, double nDob, string nStr)
        {
            Console.WriteLine($"包含3个参数:{nInt},{nDob},{nStr}");
        }

        private static event Func<int, double, string> TestFunc1;


        private static event Func<int, double> TestFunc2;


        private static string fun_1(int nInt, double dDouble)
        {
            Console.WriteLine($"参数{nInt},{dDouble}");

            return "事件1";
        }

        private static string fun_2(int nInt, double dDouble)
        {
            Console.WriteLine($"参数{nInt},{dDouble}");

            return "事件2";
        }

        static void Main(string[] args)
        {
            TestFunc1 += fun_1;
            TestFunc1 += fun_2;


            var t = TestFunc1.Invoke(1, 3);

            Console.WriteLine(t);


            action_0 += Fun_0;
            action_1 += Fun_1;
            action_2 += Fun_2;
            action_3 += Fun_3;


            action_0.Invoke();
            action_1.Invoke(1);
            action_2.Invoke(1, 2);
            action_3.Invoke(1, 2, "3");


            //Math math;//声明委托
            //math = new Math(Add); //创建委托对象，与指定进行关联
            //var k = math(3, 4);//调用委托函数


            //Stopwatch stopwatch = new Stopwatch();

            //Task.Delay(2000).ContinueWith(s =>
            //{
            //    stopwatch.Stop();
            //    Console.WriteLine($"delay之后消耗时间：{ stopwatch.ElapsedMilliseconds}");
            //    Console.WriteLine("这是一个延迟任务");

            //});
            //int Ref = 0;

            //for (int i = 0; i < 10; i++)
            //{
            //    Interlocked.Increment(ref Ref);
            //}

            for (int a = 0; a < 10; a++)
            {
                m_ResultMap.TryAdd(a, a.ToString());

                if (m_ResultMap.ContainsKey(a))
                {
                    var map = m_ResultMap[a];
                }


                Thread.Sleep(100);
            }



            Console.Read();

        }

        private static string Program_TestFunc1(int arg1, double arg2)
        {
            throw new NotImplementedException();
        }
    }
}
